var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');



var index = require('./server/routes/index');
var users = require('./server/routes/users');

var api = require('./server/api/api');

var app = express();


var server = require('http').Server(app);
var io = require('socket.io')(server);


// view engine setup
// app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'dist')));

var socketHadel = require('./server/socket'); 
socketHadel(io,app);

// app.use(expressJwt({
//     secret: 'zyc',
//     credentialsRequired: false,
//     getToken: function fromHeaderOrQuerystring(req, res) {

//         if (req.headers.token) {
//             return req.headers.token;
//         } else if (req.query && req.query.token) {
//             return req.query.token;
//         }

//         console.log('not find token');

//         return null;
//     }
// }).unless({ path: ['/api/sign', '/api/login'] }));

app.use('/api', api);
app.use('/', index);
app.use('/users', users);
// app.get('*', function (req, res, next) {

//     res.sendFile(__dirname + "/dist/index.html");
// });



// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

//设置跨域访问
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*'); //允许所有域
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST'); //您可以发送  POST和GET请求此服务。
    //X-Requested-With 并content-type 允许头文件。
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
    res.header("Content-Type", "application/json;charset=utf-8");
    res.setHeader('X-Frame-Options', 'SAMEORIGIN');
    next();
});




server.listen(3000, function () {
    console.log('listen on port 3000');
});